/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.activity.intent;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

/**
 * Holds task results
 * 
 * @author Justas
 */
public class TaskIntentHolder extends IntentHolder {

    private static final String PARAMS = "criteria";
    private static final String STATUS = "status";
    private static final String RESULT = "result";
    private static final String RESULT_MESSAGE = "errorMessage";

    public TaskIntentHolder(Intent intent) {
        super(intent);
    }

    public TaskIntentHolder(Bundle bundle) {
        super(bundle);
    }

    public TaskIntentHolder(String broadcastAction) {
        super(broadcastAction);
    }
    
    @SuppressWarnings("unchecked")
    public <T extends Parcelable> T[] getParams() {
        return (T[]) intent.getParcelableArrayExtra(PARAMS);
    }
    
    public String[] getStringParams() {
        return intent.getStringArrayExtra(PARAMS);
    }
    
    public <T extends Parcelable> void setParams(T... params) {
        intent.putExtra(PARAMS, params);
    }
    
    public void setParams(String... params) {
        intent.putExtra(PARAMS, params);
    }
    
    public int getStatus() {
        return intent.getIntExtra(STATUS, 0);
    }
    
    public void setStatus(int status) {
        intent.putExtra(STATUS, status);
    }

    public int getResultMessage() {
        return intent.getIntExtra(RESULT_MESSAGE, 0);
    }
    
    public void setResultMessage(int errorMessage) {
        intent.putExtra(RESULT_MESSAGE, errorMessage);
    }

    public <T extends Parcelable> void setResult(T result) {
        intent.putExtra(RESULT, result);
    }

    public void setResult(List<String> result) {
        if (result != null) {
            intent.putExtra(RESULT, result.toArray(new String[result.size()]));
        }
    }
    
    public List<String> getStringListResult() {
        String[] value = intent.getStringArrayExtra(RESULT);
        
        if (value == null) {
            return null;
        }
        return Arrays.asList(value);
    }

    public <T extends Parcelable> T getResult() {
        return intent.getParcelableExtra(RESULT);
    }

    public <T extends Parcelable> void setResult(Collection<T> result, Class<T> clazz) {
        setCollection(RESULT, result, clazz);
    }

    public <T extends Parcelable> List<T> getListResult() {
        return getList(RESULT);
    }
}
